import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 马拉圈
 * Date: 2023-03-28
 * Time: 21:05
 */
public class Test {


    public static void main3(String[] args) throws InterruptedException {
        AtomicInteger atomicInteger = new AtomicInteger(0);
        //只要引用指向不改变，就可以被lambda表达式捕获~
        Thread thread1 = new Thread(() -> {
            for (int i = 0; i < 50000; i++) {
                atomicInteger.getAndIncrement();
            }
        });
        Thread thread2 = new Thread(() -> {
            for (int i = 0; i < 50000; i++) {
                atomicInteger.getAndIncrement();
            }
        });
        thread1.start();
        thread2.start();
        thread1.join();
        thread2.join();
        System.out.println(atomicInteger.get());//获取对应值~
    }



    public static void main2(String[] args) {
        AtomicInteger atomicInteger = new AtomicInteger(0);
        //没有初始化后续就会出错,例如int a; a++; 这会报错
        atomicInteger.getAndIncrement();
        //先get再increment，后置加加
        atomicInteger.incrementAndGet();
        //先increment再get，前置加
///////////////////////////////////////////////////////////////////
        atomicInteger.getAndDecrement();
        //先get再decrement，后置减减
        atomicInteger.decrementAndGet();
        //先decrement再get，前置减减

    }


    public static void main1(String[] args) throws InterruptedException {
        StringBuilder stringBuilder = new StringBuilder();
        StringBuffer stringBuffer = new StringBuffer();
        Thread t1 = new Thread(() -> {
            for (int i = 0; i< 5000; i++) {
                stringBuffer.append(1);
                stringBuilder.append(1);
            }
        });
        Thread t2 = new Thread(() -> {
            for (int i = 0; i < 5000; i++) {
                stringBuffer.append(1);
                stringBuilder.append(1);
            }
        });
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println(stringBuilder.length());
        System.out.println(stringBuffer.length());

    }
}
